software master at the intersection of technology, science and art

home

download

extension methods


Extension methods which are not part of an existing type but may be called as if they were part of the type. Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.


The most common extension methods are the LINQ standard query operators that add query functionality to the existing System.Collections.IEnumerable and System.Collections.Generic.IEnumerable types. To use the standard query operators, first bring them into scope with a using System.Linq directive. Then any type that implements IEnumerable appears to have instance methods such as GroupBy, OrderBy, Average, and so on. You can see these additional methods in IntelliSense statement completion when you type "dot" after an instance of an IEnumerable type such as List or Array.


Another area of use is in HtmlHelpers in MVC


See Extension Methods (C# Programming Guide) for a detailed discussion.